package com.joe.updemo.utils;

import android.annotation.SuppressLint;
import android.os.Environment;
import com.joe.updemo.BuildConfig;

import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.joe.updemo.utils.PayContext.IMG_DIR_NAME;
import static com.joe.updemo.utils.PayContext.LOG_DIR_NAME;

/**
 * Created by Joe on 2017/09/11 15:02.
 */

public class PayFileUtil {
    private static final ThreadLocal<SimpleDateFormat> sDateFormat = new ThreadLocal<SimpleDateFormat>() {
        @SuppressLint("SimpleDateFormat")
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
    public static final String IMG_DIR_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() +
            File.separator + BuildConfig.APPLICATION_ID + File.separator + IMG_DIR_NAME;

    /**
     * @param message content
     */
    public static void saveLogFile(String message) {
        message = stringNowTime() + " " + message + "\n";
        String fileName = "log_" + sDateFormat.get().format(new Date()) + ".log";
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() +
                    File.separator + BuildConfig.APPLICATION_ID;
            createDir(storagePath);
            String path = storagePath + File.separator + LOG_DIR_NAME;
            createDir(path);
            File file1 = new File(path, fileName);
            FileWriter fileWriter1 = null;
            try {
                fileWriter1 = new FileWriter(file1, true);
                fileWriter1.write(message);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                quietClose(fileWriter1);
            }
        }
    }

    /**
     * save ScreenShot file
     */
    public static void saveScreenShot(String name) {
        try {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() +
                        File.separator + BuildConfig.APPLICATION_ID;
                createDir(storagePath);
                createDir(IMG_DIR_PATH);
                deleteDirFile(IMG_DIR_PATH);
                ActivityHelper.screenShot(IMG_DIR_PATH, name);
            }
        } catch (Exception e) {
            LogUtil.log("saveScreenShot Exception" + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void quietClose(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String stringNowTime() {
        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }

    /**
     * @param path target dir path
     * @return boolean
     */
    //创建sd上文件夹
    private static boolean createDir(String path) {
        File dirFile = new File(path);
        return dirFile.exists() || dirFile.mkdir();
    }

    /**
     * @param path     target dir path
     * @param fileName target file name
     * @return boolean
     */
    private static boolean delFile(String path, String fileName) {
        File file = new File(path, fileName);
        return file.isFile() && file.exists() && file.delete();
    }

    public static void deleteDirFile(String path) {
        File dir = new File(path);
        if (!dir.exists() || !dir.isDirectory())
            return;

        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除�?��文件
            else if (file.isDirectory())
                deleteDir(path); // 递规的方式删除文件夹
        }
    }

    private static void deleteDir(String path) {
        File dir = new File(path);
        if (dir == null || !dir.exists() || !dir.isDirectory())
            return;

        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除�?��文件
            else if (file.isDirectory())
                deleteDir(path); // 递规的方式删除文件夹
        }
        dir.delete();// 删除目录本身
    }
}
